<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      class="formList"
    >
      <el-row>
        <el-form-item label="业务编号" prop="bizNo">
          <el-input
            v-model="queryParams.bizNo"
            placeholder="请输入业务编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="模板编号" prop="templateNo">
          <el-input
            v-model="queryParams.templateNo"
            placeholder="请输入模板编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="模板标题" prop="templateTitle">
          <el-input
            v-model="queryParams.templateTitle"
            placeholder="请输入模板标题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="会员ID" prop="memberUid">
          <el-input
            v-model="queryParams.memberUid"
            placeholder="请输入会员ID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="openID" prop="memberOpenId">
          <el-input
            v-model="queryParams.memberOpenId"
            placeholder="请输入openID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


        <el-form-item label="会员昵称" prop="memberNickName">
          <el-input
            v-model="queryParams.memberNickName"
            placeholder="请输入会员昵称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

      </el-row>
      <el-row>
        <el-form-item label="发送状态" prop="sendStatus">
          <el-select v-model="queryParams.sendStatus" placeholder="请选择发送状态" clearable size="small">
            <el-option
              v-for="dict in sendStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>


        <el-form-item label="发送结果" prop="messageResult">
          <el-input
            v-model="queryParams.messageResult"
            placeholder="请输入发送结果"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


        <el-form-item label="发送时间" prop="sendTime">
          <el-date-picker clearable size="small"
                          v-model="queryParams.sendTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择发送时间">
          </el-date-picker>
        </el-form-item>

      </el-row>
      <el-row>
        <el-form-item style="margin-left: 22px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
    <el-row  :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wechat:wechatMessage:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    </el-row>

    <div class="divTable">
    <el-table  border max-height v-loading="loading" :data="wechatMessageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="模板标题" align="center" prop="templateTitle" />
      <el-table-column label="业务编号" align="center" prop="bizNo"  />
      <el-table-column label="模板编号" align="center" prop="templateNo"  />
      <el-table-column label="会员昵称" align="center" prop="memberNickName"  />
      <el-table-column label="会员ID" align="center" prop="memberUid"  />
      <el-table-column label="openID" align="center" prop="memberOpenId"  />
      <el-table-column label="消息内容" align="center" prop="messageBody"  />
      <el-table-column label="发送状态" align="center" prop="sendStatusStr" :formatter="sendStatusFormat" />
      <el-table-column label="发送结果" align="center" prop="messageResult"  />
      <el-table-column label="发送时间" align="center" prop="sendTime" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </div>

    <!-- 添加或修改消息发送记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看消息发送记录对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
             <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
          </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listWechatMessage,
    getWechatMessage,
    delWechatMessage,
    addWechatMessage,
    updateWechatMessage
  } from "@/api/wechat/wechatMessage";

  export default {
    name: "WechatMessage",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 消息发送记录表格数据
        wechatMessageList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 发送状态字典
        sendStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          bizNo: null,
          templateNo: null,
          templateTitle: null,
          memberUid: null,
          memberOpenId: null,
          memberNickName: null,
          messageBody: null,
          sendStatus: null,
          messageResult: null,
          sendTime: null,
        },

        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("wechat_information_state").then(response => {
        this.sendStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询消息发送记录列表 */
      getList() {
        this.loading = true;
        listWechatMessage(this.queryParams).then(response => {
          this.wechatMessageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 发送状态字典翻译
      sendStatusFormat(row, column) {
        return this.selectDictLabel(this.sendStatusOptions, row.sendStatusStr);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          messageUid: null,
          storeUid: null,
          bizNo: null,
          templateNo: null,
          templateTitle: null,
          memberUid: null,
          memberOpenId: null,
          memberNickName: null,
          messageBody: null,
          sendStatus: null,
          messageResult: null,
          sendTime: null,
          sendUser: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.messageUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加消息发送记录";
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const messageUid = row.messageUid || this.ids
        getWechatMessage(messageUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改消息发送记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.messageUid != null) {
              updateWechatMessage(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWechatMessage(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const messageUids = row.messageUid || this.ids;
        this.$confirm('是否确认删除消息发送记录编号为"' + messageUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delWechatMessage(messageUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wechat/wechatMessage/export', {
          ...this.queryParams
        }, `wechat_wechatMessage.xlsx`)
      }
    }
  };
</script>
